﻿using FastCloud.Base.Config;
using FastIOC.Attribute;
using FastORM.Applicaiton;
using FastORM.Entity;
using FastORM.Enum;
using FastUtil.Config;
using FastUtil.Convert;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using static FastIOC.Enum.CotainerEnum;

namespace FastCloud.Base.Desgin
{
    [Component(Scope = ComponentScope.Prototype)]
    public class BaseML<TEntity> : IRepository<TEntity> where TEntity : BaseEntity
    {
        [AutoWired]
        public CommonDTO DTO;
        public TokenUtil TokenUtil;
        private CommonDao CommonDao;
        public string UserGuid { set; get; }
        public string DisplayName { set; get; }
        public string OUGuid { set; get; }
        public string OUName { set; get; }
        public string OUCode { set; get; }
        public string Mobile { set; get; }
        public BaseML()
        {
            @CommonDao = new CommonDao(DataBaseClass.MySql, CloudUtil.GetBaseConnectString());
            TokenUtil = (TokenUtil)InjectContext.Resolve(typeof(TokenUtil));
            try
            {
                TokenEntity Entity = TokenUtil.GetTokenEntity();
                UserGuid = Entity.UserGuid;
                DisplayName = Entity.DisplayName;
                OUGuid = Entity.OUGuid;
                OUName = Entity.OUName;
                OUCode = Entity.OUCode;
                Mobile = Entity.Mobile;
            }
            catch (Exception)
            {

            }
        }

        public void ChangeConnectString(string ConnectString)
        {
            @CommonDao = new CommonDao(DataBaseClass.MySql, ConnectString);
        }

        public virtual void Insert(TEntity Entity)
        {
            @CommonDao.Insert(Entity);
        }

        public virtual void InsertBatch(List<TEntity> EntityList)
        {
            @CommonDao.InsertBatch(EntityList);
        }

        public virtual void DeleteByID(string UnitGuid)
        {
            @CommonDao.Delete<TEntity>(UnitGuid);
        }

        public virtual void DeleteByFiled(string FieldName,string FiledValue)
        {
            @CommonDao.Delete<TEntity>(FieldName, FiledValue);
        }

        public virtual void DeleteBatch(List<string> UnitGuidList)
        {
            foreach (string UnitGuid in UnitGuidList)
            {
                DeleteByID(UnitGuid);
            }
        }

        public virtual void UpdateByID(TEntity Entity)
        {
            @CommonDao.Update(Entity);
        }

        public virtual void UpdateByFieldName(TEntity Entity,string FieldName)
        {
            @CommonDao.Update(Entity, FieldName);
        }

        public virtual void UpdateBatch(List<TEntity> EntityList)
        {
            foreach (TEntity Entity in EntityList)
            {
                UpdateByID(Entity);
            }
        }

        public virtual TEntity SelectOne(string UnitGuid)
        {
            return @CommonDao.Find<TEntity>(UnitGuid);

        }
    }
}
